home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8647 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.1 KB

  1. Path: newshub.cts.com!usenet
  2. From: lboard@cts.com (Larry Board)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: What is referencing good for?
  5. Date: Sun, 25 Feb 1996 20:22:41 GMT
  6. Organization: CTS Network Services
  7. Message-ID: <3130c026.8571250@news2.cts.com>
  8. References: <4goojd$a9g@wintermute.ecs.fullerton.edu> <4goutn$31u@news.bridge.net>
  9. NNTP-Posting-Host: lboard.cts.com
  10.  
  11. On 25 Feb 1996 06:17:27 GMT, David Byrden <100101.2547@compuserve.com>
  12. wrote:
  13.  
  14. >
  15. >
  16. >
  17. > Gil;
  18. >
  19. >
  20. >>> Can someone give me a REAL world example of where I would use 
  21. >>> referencing perhaps somewhere that I can not use pointers?
  22. >
  23. >
  24. > References are necessary to allow operator overloading.
  25. >Consider this;
  26. >
  27. >
  28. >class array {
  29. >private:
  30. >    int * dataBlock ;
  31. >public :
  32. >    int& operator[] ( int index )
  33. >    {
  34. >        return dataBlock [ index ] ;
  35. >    }
  36. >    // constructor etc etc
  37. >} ;
  38. >
  39. >
  40. >
  41. >    array  ar ;
  42. >    ar[4] = 0 ;      // can't be done with pointers
  43. >
  44. >
  45.  
  46. Good example, but.... the next question begs to be answered.  Why
  47. would anybody want to do this?  It essentially gives public access to
  48. datablock, which is a private variable.
  49.  
  50.  
  51.